//
//  YWAttendanceRecordsCell.m
//  maintain
//
//  Created by Kyo on 16/9/28.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWAttendanceRecordsCell.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "YYLabel.h"
#import "YWCommonTool.h"

#import "YWAttendanceRecord.h"
#import "YWHandler.h"
#import "YWLocation.h"
#import "YWBuildingManager.h"

@interface YWAttendanceRecordsCell()
/** 考勤人 */
@property (nonatomic, strong) YYLabel *executorLabel;
/** 签到，签退 */
@property (nonatomic, strong) YYLabel *typeLabel;
/** GPS */
@property (nonatomic, strong) YYLabel *GPSLabel;
/** 位置 */
@property (nonatomic, strong) YYLabel *locationLabel;
/** 维修时间 */
@property (nonatomic, strong) YYLabel *timeLabel;
/** 说明 */
@property (nonatomic, strong) YYLabel *noteLabel;
@end

@implementation YWAttendanceRecordsCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setAttendanceRecord:(YWAttendanceRecord *)attendanceRecord{
    _attendanceRecord = attendanceRecord;
    NSString *handler = attendanceRecord.user.userName;
    //[_executorLabel labelText:@"签到人: " withAttributedText:handler.length?handler:@"无" andAttributedColor:[UIColor customYellowColor]];
    _executorLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"签到人: " firstColor:[UIColor customTextGrayBlackColor] secondText:handler.length?handler:@"无" secondColor:[UIColor customYellowColor] font:kContentFont];
    
    NSString* type = self.attendanceRecord.type == YWAttendanceTypeArrive ? @"签到" : @"签退";
    _typeLabel.text = type;
    
    NSString *locationText = [[YWBuildingManager shareManager] areaLocationWithLocationId:attendanceRecord.location.locationId];
    //[_locationLabel labelText:@"位置: " withAttributedText:locationText.length?locationText:@"无" andAttributedColor:[UIColor blackColor]];
    _locationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"位置: " firstColor:[UIColor customTextGrayBlackColor] secondText:locationText.length?locationText:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    
    NSString *gpsLocaiton = attendanceRecord.locationText.length>0?attendanceRecord.locationText : @"无";
    //[_GPSLabel labelText:@"GPS位置: " withAttributedText:gpsLocaiton andAttributedColor:[UIColor blackColor]];
    _GPSLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"GPS位置: " firstColor:[UIColor customTextGrayBlackColor] secondText:gpsLocaiton secondColor:[UIColor blackColor] font:kContentFont];
    
    NSString *time = [YWCommonTool showTime:attendanceRecord.arriveTime/1000 andFormatter:KTimeFormatShortTimeDate];
    //[_timeLabel labelText:@"签到时间: " withAttributedText:time.length?time:@"无" andAttributedColor:[UIColor customYellowColor]];
    _timeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"签到时间: " firstColor:[UIColor customTextGrayBlackColor] secondText:time.length?time:@"无" secondColor:[UIColor customYellowColor] font:kContentFont];
    
    //[_noteLabel labelText:@"备注: " withAttributedText:attendanceRecord.arriveRemark.length?attendanceRecord.arriveRemark:@"无" andAttributedColor:[UIColor blackColor]];
    _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"备注: " firstColor:[UIColor customTextGrayBlackColor] secondText:attendanceRecord.arriveRemark.length?attendanceRecord.arriveRemark:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    
    _GPSLabel.preferredMaxLayoutWidth = _GPSLabel.frame.size.width;
    _locationLabel.preferredMaxLayoutWidth = _locationLabel.frame.size.width;
    _noteLabel.preferredMaxLayoutWidth = _noteLabel.frame.size.width;
}

- (void)setUpSubviews {
    
    UIView *superview = self.contentView;
    [self.executorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superview.mas_top).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-46);
    }];
    
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_executorLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    [self.GPSLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_locationLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_GPSLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_timeLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
        make.bottom.mas_equalTo(superview.mas_bottom).offset(-8);
    }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.baseline.mas_equalTo(_executorLabel);
        make.bottom.mas_equalTo(_executorLabel.mas_bottom);
        make.right.mas_equalTo(superview).offset(-8);
        make.width.mas_equalTo(@32);
        make.height.mas_equalTo(@16);
    }];
}


- (YYLabel *)executorLabel{
    if (!_executorLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _executorLabel = label;
        [self.contentView addSubview:_executorLabel];
    }
    return _executorLabel;
}

- (YYLabel *)typeLabel{
    if (!_typeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _typeLabel = label;
        [self.contentView addSubview:_typeLabel];
    }
    return _typeLabel;
}

- (YYLabel *)GPSLabel{
    if (!_GPSLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayBlackColor];
        _GPSLabel = label;
        [self.contentView addSubview:_GPSLabel];
    }
    return _GPSLabel;
}

- (YYLabel *)locationLabel{
    if (!_locationLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayBlackColor];
        _locationLabel = label;
        [self.contentView addSubview:_locationLabel];
    }
    return _locationLabel;
}

- (YYLabel *)timeLabel{
    if (!_timeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _timeLabel = label;
        [self.contentView addSubview:_timeLabel];
    }
    return _timeLabel;
}

- (YYLabel *)noteLabel{
    if (!_noteLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayBlackColor];
        _noteLabel = label;
        [self.contentView addSubview:_noteLabel];
    }
    return _noteLabel;
}

@end
